package me.gacl.demo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import org.junit.Test;

import me.gacl.utils.JdbcUtils;

public class TestGetAutoKey {

	@Test
	public void test() {
		Connection conn = null;
		PreparedStatement st = null;
		ResultSet rs = null;
		try {
			conn = JdbcUtils.getConnection();
			String sql = "insert into test1(name) values(?)";
			st = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
			st.setString(1, "aaa");
			st.executeUpdate();
			//获取数据库自动生成的主键
			rs = st.getGeneratedKeys();
			if(rs.next()){
                System.out.println(rs.getInt(1));
            }
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			JdbcUtils.release(conn, st, rs);
		}
	}
}
